package vikaa.timelinepackage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vikaa.pullview.PullToRefreshImgListView;
import com.vikaa.pullview.PullToRefreshImgListView.OnRefreshListener;
import com.vikaa.wowo.widget.MyAdapterView;

import vikaa.framework.UIHelper;
import vikaa.timelinepackage.TimelineSettings.UpdateSettingsThread;
import vikaa.tools.DialogService;
import vikaa.tools.HttpUtil;
import vikaa.tools.Logger;
import vikaa.tools.StringUtils;
import vikaa.tools.ValueClass;
import vikaa.ui.Mywowo;
import vikaa.ui.WowoMember;
import vikaa.wowo.R;
import vikaa.wowo.cache.DefaultValueSharePreference;
import vikaa.wowo.cache.SpaceListSqlStoragePrivate;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.LiveFolders;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;


public class Timeline extends Activity implements OnItemClickListener,  OnGestureListener , OnDoubleTapListener {
	private final static int ACCEPT_ACCESS = 101;
	private final static int ACCEPT_FAIL = 102;
	private final static int JOIN_ACCESS = 103;
	private final static int JOIN_FAIL = 104;
	
	private final static int IMPORTBTN = 101;
	private final static int CALLBTN = 102;
	private final static int MEMORYBTN = 103;
	private final static int DYNAMICBTN = 104;
	private ArrayList<Timeline_item> tlitem = new ArrayList<Timeline_item>();
	private ArrayList<Timeline_item> itemlist = new ArrayList<Timeline_item>() ;
	private AdapterTimelineItem adapter;
	public static PinnedHeaderListView listView;
	public static PullToRefreshImgListView listview2;
	public static int page;
	public static String space_id;
	public static Timeline tl;
	public static int count;
    public static String memberNum;
    private String credits;
    public static String space_name;
    public static String managerCount;
    private int accept;
    private ImageView settingsBtn;
    private ImageView spacelistBtn;
    private ImageView addBtn;
    private RelativeLayout tipRl;
    private ImageView joinBtn;
    private RelativeLayout bottomImg;
    private ImageView dynamicBtn;
    private ImageView memoryBtn;
    private ImageView importBtn;
    private ImageView callBtn;
    private TextView titleTv;
    private String coverUrl;
    public static String role;
    public static String state;
    
    public static boolean isrefresh = false;
    public static boolean isimport = false;
    public static boolean isfinish = false;
    private boolean singleclick = false;
    private boolean doubleclick = false;
    private boolean fling = false;
    
    private GestureDetector mGestureDetector;  
    private int verticalMinDistance = 200;  
    private int minVelocity         = 0;  
    
    public static  View footerview;
    public static RelativeLayout foot;
    
    public static boolean dynamicFlag ;
    public static int dypage;
    
    private RelativeLayout guide1;
    private RelativeLayout guide2;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeline);
        role = getIntent().getExtras().getString("role");
        space_id = getIntent().getExtras().getString("sid");
        count = Integer.valueOf(getIntent().getExtras().getString("count"));
        space_name = getIntent().getExtras().getString("spacename");
        coverUrl = getIntent().getExtras().getString("cover");
        credits = SpaceListSqlStoragePrivate.getCredits(space_id, getIntent().getExtras().getString("uid"), Timeline.this);
        memberNum = getIntent().getExtras().getString("member");
        managerCount = getIntent().getExtras().getString("host");
        state = getIntent().getExtras().getString("state");
        accept = getIntent().getExtras().getInt("accept");
        tl = this;
        isrefresh = false;
        isfinish = false;
        tipRl = (RelativeLayout)findViewById(R.id.tipRl);
        initview();
    	initlistview2();
    	initdata();
        mGestureDetector = new GestureDetector((OnGestureListener) this);
        guide1 = (RelativeLayout)findViewById(R.id.guide1);
        guide2 = (RelativeLayout)findViewById(R.id.guide2);
        if(!DefaultValueSharePreference.getguidetimeline(Timeline.this)  ){
        	guide1.setVisibility(View.VISIBLE);
            guide1.setOnClickListener(new OnClickListener() {
    			public void onClick(View v) {
    				guide1.setVisibility(View.GONE);
    				guide2.setVisibility(View.VISIBLE);
    			}
    		});
            guide2.setOnClickListener(new OnClickListener() {
            	public void onClick(View v) {
            		guide2.setVisibility(View.GONE);
            		DefaultValueSharePreference.putguidetimeline(Timeline.this, true);
    			}
    		});
        }
        
    }
	@Override
	protected void onDestroy() {
		super.onDestroy();
		System.gc();
	}
	@Override
	protected void onResume() {
		super.onResume();
		singleclick = false;
		doubleclick = false;
		if(isrefresh){
			listview2.setSelection(0);
			listview2.loadingAni.setVisibility(View.VISIBLE);
			listview2.state = listview2.REFRESHING;
			listview2.changeHeaderViewByState();
			Toast toast = new Toast(Timeline.this);
        	toast = Toast.makeText(Timeline.this, getString(R.string.published), 1000);
        	toast.setGravity(Gravity.CENTER, 0, 0);
        	toast.show();
        	isrefresh = false;
        	adapter.ran = true;
        	dynamicFlag = false;
			new TimelineNewThread("1", space_id, tlitem.get(0).getDateline(), 7).start();
			new GetWOWOInfoThread(space_id).start();
		}
		if(isimport){
			listview2.setSelection(0);
			listview2.loadingAni.setVisibility(View.VISIBLE);
			listview2.state = listview2.REFRESHING;
			listview2.changeHeaderViewByState();
			/*Toast toast = new Toast(Timeline.this);
        	toast = Toast.makeText(Timeline.this, getString(R.string.published), 1000);
        	toast.setGravity(Gravity.CENTER, 0, 0);
        	toast.show();*/
        	isimport = false;
        	adapter.ran = true;
        	dynamicFlag = false;
			new TimelineImportThread("1", space_id,  9).start();
			new GetWOWOInfoThread(space_id).start();
		}
		if(isfinish){
			finish();
		}
		
		titleTv.setText(space_name);
		if(state.equals("1") ){
			listview2.ll.setVisibility(View.GONE);
			listview2.myContributionMsg.setText("这里已记录我们");
			listview2.myContributionNumber.setText(String.valueOf(count));
			listview2.myContributionMsg2.setVisibility(View.VISIBLE);
		}else{
			listview2.myContributionNumber.setText(credits);
			listview2.memberNumTv.setText(memberNum);
			listview2.ll.setVisibility(View.VISIBLE);
			listview2.myContributionMsg.setText("我的贡献度:");
			listview2.myContributionNumber.setText(credits);
			listview2.myContributionMsg2.setVisibility(View.GONE);
		}
		if(state.equals("1") || Integer.valueOf(role) == ValueClass.ROLE_CREATOR || Integer.valueOf(role) == ValueClass.ROLE_ADMIN){
			botomMenu4private();
			if(dynamicFlag){
				dynamicBtn.setSelected(true);
				memoryBtn.setSelected(false);
			}else{
				dynamicBtn.setSelected(false);
				memoryBtn.setSelected(true);
			}
		}else{
			botomMenu4public();
			
		}
		if(Integer.valueOf(role) == ValueClass.ROLE_INVITED || Integer.valueOf(role) == ValueClass.ROLE_0){
			joinBtn.setVisibility(View.VISIBLE);
			bottomImg.setVisibility(View.VISIBLE);
			addBtn.setVisibility(View.GONE);
			tipRl.setVisibility(View.GONE);
			settingsBtn.setVisibility(View.INVISIBLE);
			listview2.conRl.setVisibility(View.INVISIBLE);
			listview2.ll.setVisibility(View.INVISIBLE);
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode != RESULT_OK) return;
		if(requestCode == 10){
			Bundle b = data.getExtras();  
			space_name = b.getString("spacename");
			state = b.getString("state");
			if(b.getInt("finish") == 1){
				Logger.i("11111");
				Mywowo.isrefresh = true;
				Mywowo.wowoexit = true;
				finish();
			}
		}
	}
	public void initview(){
		titleTv = (TextView)findViewById(R.id.titleTv);
		settingsBtn = (ImageView)findViewById(R.id.settingsBtn);
		settingsBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(Timeline.this,TimelineSettings.class);
				i.putExtra("space_id", space_id);
				i.putExtra("role", getIntent().getExtras().getString("role"));
				i.putExtra("state", state);
				startActivityForResult(i, 10);
			}
		});
		spacelistBtn = (ImageView)findViewById(R.id.spacelistBtn);
		spacelistBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
				//TwitterSqlStoragePrivate.dropmyTwitter(getApplicationContext());
				//TwitterSqlStoragePrivate.dropfilelistTwitter(getApplicationContext());
			}
		});
		tipRl.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
			}
		});
		addBtn = (ImageView)findViewById(R.id.addBtn);
		addBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(Timeline.this,TimelineNewMemory.class);
				i.putExtra("space_id", space_id);
				startActivity(i);
			}
		});
		joinBtn = (ImageView)findViewById(R.id.joinBtn);
		joinBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(accept == 0){
					DialogService.showProgressDialog(Timeline.this, "加入中");
					new JoinThread().start();
				}
				else{
					DialogService.showProgressDialog(Timeline.this, "接受邀请中");
					new AcceptThread().start();
				}
			}
		});
		bottomImg = (RelativeLayout)findViewById(R.id.bottomImg);
	}
	private void botomMenu4private(){
		tipRl.removeAllViews();
		importBtn = new ImageView(this);
		importBtn.setId(IMPORTBTN);
		importBtn.setImageResource(R.drawable.btn_timeline_import);
		importBtn.setOnClickListener(new BotomMenulListener());
		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		params1.addRule(RelativeLayout.CENTER_VERTICAL);
		params1.setMargins(0, 0, UIHelper.dip2px(this, 22), 0);
		importBtn.setLayoutParams(params1);
		tipRl.addView(importBtn);
		
		callBtn = new ImageView(this);
		callBtn.setId(CALLBTN);
		callBtn.setImageResource(R.drawable.btn_timeline_call);
		callBtn.setOnClickListener(new BotomMenulListener());
		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params2.addRule(RelativeLayout.CENTER_VERTICAL);
		params2.addRule(RelativeLayout.LEFT_OF, importBtn.getId());
		params2.setMargins(0, 0, UIHelper.dip2px(this, 35) 	, 0);
		callBtn.setLayoutParams(params2);
		tipRl.addView(callBtn);
		
		dynamicBtn = new ImageView(this);
		dynamicBtn.setId(DYNAMICBTN);
		dynamicBtn.setImageResource(R.drawable.btn_timeline_dynamic);
		dynamicBtn.setOnClickListener(new BotomMenulListener());
		RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params3.addRule(RelativeLayout.CENTER_VERTICAL);
		params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		params3.setMargins(UIHelper.dip2px(this, 20), 0, 0, 0);
		dynamicBtn.setLayoutParams(params3);
		tipRl.addView(dynamicBtn);
		
		memoryBtn = new ImageView(this);
		memoryBtn.setId(MEMORYBTN);
		memoryBtn.setImageResource(R.drawable.btn_timeline_memory);
		memoryBtn.setOnClickListener(new BotomMenulListener());
		memoryBtn.setSelected(true);
		RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params4.addRule(RelativeLayout.CENTER_VERTICAL);
		params4.addRule(RelativeLayout.RIGHT_OF, dynamicBtn.getId());
		params4.setMargins(UIHelper.dip2px(this, 34), 0, 0, 0);
		memoryBtn.setLayoutParams(params4);
		tipRl.addView(memoryBtn);
	}
	private void botomMenu4public(){
		tipRl.removeAllViews();
		importBtn = new ImageView(this);
		importBtn.setId(IMPORTBTN);
		importBtn.setImageResource(R.drawable.btn_timeline_import);
		importBtn.setOnClickListener(new BotomMenulListener());
		RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		params1.addRule(RelativeLayout.CENTER_VERTICAL);
		params1.setMargins(0, 0, UIHelper.dip2px(this, 55), 0);
		importBtn.setLayoutParams(params1);
		tipRl.addView(importBtn);
		
		callBtn = new ImageView(this);
		callBtn.setId(CALLBTN);
		callBtn.setImageResource(R.drawable.btn_timeline_call);
		callBtn.setOnClickListener(new BotomMenulListener());
		RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params2.addRule(RelativeLayout.CENTER_VERTICAL);
		params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		params2.setMargins(UIHelper.dip2px(this, 58), 0, 0 	, 0);
		callBtn.setLayoutParams(params2);
		tipRl.addView(callBtn);
	}
	class BotomMenulListener implements OnClickListener{
		public void onClick(View v) {
			switch (v.getId()) {
			case IMPORTBTN:
				Intent i = new Intent(Timeline.this,TimelineImport.class);
				i.putExtra("space_id", space_id);
				i.putExtra("role", getIntent().getExtras().getString("role"));
				i.putExtra("state", state);
				startActivity(i);
				break;
			case CALLBTN:
				startActivity(new Intent(Timeline.this,WowoMember.class));
				break;
			case MEMORYBTN:
				adapter.ran = true;
				dynamicFlag = false;
				dynamicBtn.setSelected(false);
				memoryBtn.setSelected(true);
				listview2.loadingAni.setVisibility(View.VISIBLE);
				//tlitem.clear();
				listview2.setSelection(0);
				listview2.state = listview2.REFRESHING;
				listview2.changeHeaderViewByState();
				page = 1;
				new TimelineThread(String.valueOf(page), space_id, 1).start();
				break;
			case DYNAMICBTN:
				adapter.ran = true;
				dynamicFlag = true;
				dynamicBtn.setSelected(true);
				memoryBtn.setSelected(false);
				listview2.loadingAni.setVisibility(View.VISIBLE);
				//tlitem.clear();
				listview2.setSelection(0);
				listview2.state = listview2.REFRESHING;
				listview2.changeHeaderViewByState();
				dypage = 1;
				new DynamicThread(String.valueOf(dypage), space_id, 1).start();
				break;
			default:
				break;
			}
		}
		
	}
	/*public void initlistview(){
		//footer
		footerview = getLayoutInflater().inflate(R.layout.timeline_loadmore, null);
		foot = (RelativeLayout)footerview.findViewById(R.id.footerview);
		View bottomview = getLayoutInflater().inflate(R.layout.timeline_botom, null);
		foot = (RelativeLayout)footerview.findViewById(R.id.footerview);
		//foot.setVisibility(View.VISIBLE);
		initialview = getLayoutInflater().inflate(R.layout.timeline_initialview, null);
		head = (RelativeLayout)initialview.findViewById(R.id.initialview);
		//adapter
		adapter = new AdapterTimelineItem(getLayoutInflater(),tlitem,Timeline.this);
		listView = (PinnedHeaderListView) findViewById(R.id.section_list_view);
		listView.addHeaderView(topview,null,false);
		listView.addHeaderView(initialview);
		listView.addFooterView(footerview, null, false);
		listView.addFooterView(bottomview);
		listView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if(mGestureDetector.onTouchEvent(event)){
					return false;
				}
				if(onSingleTapConfirmed(event)){
					return false;
				}			
				return false;
			}
		});
		listView.setClickable(true);
		listView.setOnItemClickListener(this);
		listView.setAdapter(adapter);
		listView.setOnScrollListener(adapter);
		listView.setPinnedHeaderView(getLayoutInflater().inflate(R.layout.timeline_list_section, listView, false));
	}*/
	public void initlistview2(){
		//footer
		footerview = getLayoutInflater().inflate(R.layout.timeline_loadmore, null);
		View bottomview = getLayoutInflater().inflate(R.layout.timeline_botom, null);
		foot = (RelativeLayout)footerview.findViewById(R.id.footerview);
		//foot.setVisibility(View.VISIBLE);
		//adapter
		adapter = new AdapterTimelineItem(getLayoutInflater(),tlitem,Timeline.this);
		adapter.ran = true;
		listview2 = (PullToRefreshImgListView) findViewById(R.id.section_list_view);
		if(!StringUtils.isEmpty(coverUrl)){
			AsyLoadImg.loadTimeBg(HttpUtil.HttpUrlComplete(coverUrl), listview2.coverImage);
		}else{
			listview2.coverImage.setBackgroundResource(R.drawable.bg_timeline_toppic_display);
		}
		listview2.addFooterView(footerview, null, false);
		listview2.addFooterView(bottomview);
		listview2.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if(mGestureDetector.onTouchEvent(event)){
					return false;
				}
				if(onSingleTapConfirmed(event)){
					return false;
				}			
				return false;
			}
		});
		listview2.setClickable(true);
		listview2.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {
				new AlertDialog.Builder(Timeline.this).setTitle(getString(R.string.tip)).setMessage( "添加到纪念集吗？")
				.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						JSONArray removeTwitters = new JSONArray();
						JSONArray addTwitters = new JSONArray();
						addTwitters.put(tlitem.get(position-2).getIdstr());
						new UpdateTwitterThread(addTwitters.toString(), removeTwitters.toString(), space_id).start();
						new GetWOWOInfoThread(space_id).start();
					}
				}).setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();
				
				return true;
			}
		});
		listview2.setOnItemClickListener(this);
		listview2.setAdapter(adapter);
		listview2.setOnScrollListener(adapter);
		listview2.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				adapter.ran = true;
				if(tlitem.size() > 0 && !dynamicFlag){
					new TimelineNewThread("1", space_id, String.valueOf((Long.valueOf(tlitem.get(0).getDateline())+1)), 7).start();
				}
				else{
					listview2.onRefreshComplete();
				}
			}
		});
	}
    public void initdata(){
    	page = 1;
    	listview2.loadingAni.setVisibility(View.VISIBLE);
    	if(state.equals("1") || Integer.valueOf(role) == ValueClass.ROLE_CREATOR || Integer.valueOf(role) == ValueClass.ROLE_ADMIN){
    		dynamicFlag = true;
    		Logger.i("dy");
    		new DynamicThread(String.valueOf(page), space_id, 1).start();
    	}
    	else{
    		Logger.i("tl");
    		new TimelineThread(String.valueOf(page), space_id, 1).start();
    	}
    }
    Handler mhandler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				listview2.loadingAni.setVisibility(View.GONE);
				foot.setVisibility(View.INVISIBLE);
				listview2.state = listview2.DONE;
				listview2.changeHeaderViewByState();
				if(msg.obj != null){
					ArrayList<Timeline_item> strings = (ArrayList<Timeline_item>) msg.obj;
					if(!strings.isEmpty()){
						tlitem.clear();
						tlitem.addAll(strings);
						//TimeLineSqlStoragePrivate.saveTimeLine(strings, space_id, Timeline.this);
						//tlitem.addAll(TimeLineSqlStoragePrivate.getTimeLine(space_id, page, Timeline.this));
					}
					adapter.notifyDataSetChanged();
					//listview2.setSelection(0);
				}
				break;
			case 2:
				listview2.loadingAni.setVisibility(View.GONE);
				foot.setVisibility(View.INVISIBLE);
				ArrayList<Timeline_item> strings = (ArrayList<Timeline_item>) msg.obj;
				if(!strings.isEmpty()){
					adapter.ran = true;
					tlitem.addAll(strings);
					//TimeLineSqlStoragePrivate.saveTimeLine(strings, space_id, Timeline.this);
					//tlitem.addAll(TimeLineSqlStoragePrivate.getTimeLine(space_id, page, Timeline.this));
					adapter.notifyDataSetChanged();
				}else{
					//adapter.ran = false;
					//listview2.removeFooterView(footerview);
					foot.setVisibility(View.GONE);
					/*Toast toast = new Toast(Timeline.this);
		        	toast = Toast.makeText(Timeline.this, getString(R.string.load_full), 1000);
		        	toast.setGravity(Gravity.CENTER, 0, 0);
		        	toast.show();*/
				}
				break;
			case 3:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.onRefreshComplete();
				listview2.state = listview2.DONE;
				listview2.changeHeaderViewByState();
				if(msg.obj != null && ((String)msg.obj).equals("没有微博")){
					adapter.ran = false;
					foot.setVisibility(View.GONE);
					//listview2.removeFooterView(footerview);
				}else if(msg.obj != null && ((String)msg.obj).equals("没有足够权限")){
					new AlertDialog.Builder(Timeline.this).setTitle(getString(R.string.tip)).setMessage("没有足够权限查看")
					.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					}).show();
					//Mywowo.isrefresh = true;
					//finish();
					//initlistview();
				}else if(msg.obj != null ){
					Toast.makeText(Timeline.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
					//initlistview();
				}else
					Toast.makeText(Timeline.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 4:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.onRefreshComplete();
				listview2.state = listview2.DONE;
				listview2.changeHeaderViewByState();
				Toast.makeText(Timeline.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
				break;
			case ACCEPT_ACCESS:
				DialogService.closeProgressDialog();
				joinBtn.setVisibility(View.INVISIBLE);
				bottomImg.setVisibility(View.INVISIBLE);
				addBtn.setVisibility(View.VISIBLE);
				listview2.memberNumTv.setText(String.valueOf(Integer.valueOf(memberNum)+1));
				settingsBtn.setVisibility(View.VISIBLE);
				listview2.conRl.setVisibility(View.VISIBLE);
				listview2.ll.setVisibility(View.VISIBLE);
				addBtn.setVisibility(View.VISIBLE);
				tipRl.setVisibility(View.VISIBLE);
				importBtn.setVisibility(View.VISIBLE);
				callBtn.setVisibility(View.VISIBLE);
				Mywowo.isrefresh = true;
				break;
			case ACCEPT_FAIL:
				adapter.ran = true;
				if(msg.obj != null && ((String)msg.obj).equals("Error_error:")){
					Mywowo.isrefresh = true;
					finish();
					//Toast.makeText(Timeline.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(Timeline.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case JOIN_ACCESS:
				DialogService.closeProgressDialog();
				joinBtn.setVisibility(View.INVISIBLE);
				bottomImg.setVisibility(View.INVISIBLE);
				addBtn.setVisibility(View.VISIBLE);
				listview2.memberNumTv.setText(String.valueOf(Integer.valueOf(memberNum)+1));
				settingsBtn.setVisibility(View.VISIBLE);
				listview2.conRl.setVisibility(View.VISIBLE);
				listview2.ll.setVisibility(View.VISIBLE);
				addBtn.setVisibility(View.VISIBLE);
				tipRl.setVisibility(View.VISIBLE);
				importBtn.setVisibility(View.VISIBLE);
				callBtn.setVisibility(View.VISIBLE);
				Mywowo.isrefresh = true;
				break;
			case JOIN_FAIL:
				adapter.ran = true;
				if(msg.obj != null){
					Toast.makeText(Timeline.this, (String)msg.obj  , Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(Timeline.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 7:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.onRefreshComplete();
				listview2.state = listview2.DONE;
				listview2.changeHeaderViewByState();
				adapter.ran = true;
				ArrayList<Timeline_item> newlist = (ArrayList<Timeline_item>) msg.obj;
				//TimeLineSqlStoragePrivate.saveTimeLine(newlist, space_id, Timeline.this);
				tlitem.addAll(0, newlist);
				count += newlist.size();
				adapter.notifyDataSetChanged();
				
				break;
			case 8://点赞
				Toast toast = new Toast(Timeline.this);
	        	toast = Toast.makeText(Timeline.this, "赞", 500);
	        	toast.setGravity(Gravity.CENTER, 0, 0);
	        	toast.show();
				break;
			case 9:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.state = listview2.DONE;
				listview2.changeHeaderViewByState();
				ArrayList<Timeline_item> nlist = (ArrayList<Timeline_item>) msg.obj;
				tlitem.clear();
				tlitem.addAll(0, nlist);
				adapter.notifyDataSetChanged();
				adapter.ran = true;
				break;
			}
		}
    };
    class TimelineThread extends Thread{
    	private String page;
    	private String space_id;
    	private int m;
    	boolean isrun = true;
		public TimelineThread(String page, String space_id, int m) {
			super();
			this.page = page;
			this.space_id = space_id;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			itemlist.clear();
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("space_id", space_id);
    			map.put("page", page);
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"space/timeline", map));
					if(jsonObj.getString("result").equals("1")){
						JSONArray jsonArr = jsonObj.getJSONArray("info");
						for(int i=0;i<jsonArr.length();i++){
							Timeline_item item = new  Timeline_item();
							item.setAvatar(jsonArr.getJSONObject(i).getString("avatar"));
							item.setThumbnail_pic(jsonArr.getJSONObject(i).getString("thumbnail_pic"));
							item.setBmiddle_pic(jsonArr.getJSONObject(i).getString("bmiddle_pic"));
							item.setOriginal_pic(jsonArr.getJSONObject(i).getString("original_pic"));
							item.setCreate_at(jsonArr.getJSONObject(i).getString("created_at"));
							item.setIdstr(jsonArr.getJSONObject(i).getString("idstr"));
							item.setLike(jsonArr.getJSONObject(i).getString("like"));
							item.setNickname(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("nickname")));
							item.setComments_count(jsonArr.getJSONObject(i).getString("comments_count"));
							item.setText(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("text")));
							item.setHash(jsonArr.getJSONObject(i).getString("hash"));
							item.setIsLike(jsonArr.getJSONObject(i).getString("isLike"));
							item.setDateline(jsonArr.getJSONObject(i).getString("dateline"));
							item.setTwitter_type(jsonArr.getJSONObject(i).getString("twitter_type"));
							itemlist.add(item);
						}
						Message msg = mhandler.obtainMessage(m);
						msg.obj = itemlist;
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					Logger.i(e);
					e.printStackTrace();
				}
    		}
    	}
    }
    class JoinThread extends Thread {
    	private boolean isrun = true;
    	@Override
    	public void run() {
    		while(isrun){
    			isrun = false;
    			try{
    				Map<String, String> map = new HashMap<String, String>();
    				map.put("space_id", space_id);
    				JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL + "space/join", map));
    				if(jsonObj.getString("result").equals("1")){
    					Message msg = mhandler.obtainMessage(JOIN_ACCESS);
						msg.sendToTarget();
    				}
    				else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(JOIN_FAIL);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	}
    }
    class AcceptThread extends Thread {
    	private boolean isrun = true;
    	@Override
    	public void run() {
    		while(isrun){
    			isrun = false;
    			try{
    				Map<String, String> map = new HashMap<String, String>();
    				map.put("space_id", space_id);
    				JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL + "space/accept", map));
    				if(jsonObj.getString("result").equals("1")){
    					Message msg = mhandler.obtainMessage(ACCEPT_ACCESS);
						msg.sendToTarget();
    				}
    				else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(ACCEPT_FAIL);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    		}
    	}
    }
    class TimelineNewThread extends Thread{
    	private String page;
    	private String space_id;
    	private String min;
    	private int m;
    	boolean isrun = true;
		public TimelineNewThread(String page, String space_id, String min, int m) {
			super();
			this.page = page;
			this.space_id = space_id;
			this.min = min;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("space_id", space_id);
    			map.put("page", page);
    			map.put("min", min);
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"space/timeline", map));
					Logger.i(jsonObj.toString());
					if(jsonObj.getString("result").equals("1")){
						JSONArray jsonArr = jsonObj.getJSONArray("info");
						itemlist = new ArrayList<Timeline_item>();
						for(int i=0;i<jsonArr.length();i++){
							Timeline_item item = new Timeline_item();
							item.setAvatar(jsonArr.getJSONObject(i).getString("avatar"));
							item.setThumbnail_pic(jsonArr.getJSONObject(i).getString("thumbnail_pic"));
							item.setBmiddle_pic(jsonArr.getJSONObject(i).getString("bmiddle_pic"));
							item.setOriginal_pic(jsonArr.getJSONObject(i).getString("original_pic"));
							item.setCreate_at(jsonArr.getJSONObject(i).getString("created_at"));
							item.setIdstr(jsonArr.getJSONObject(i).getString("idstr"));
							item.setLike(jsonArr.getJSONObject(i).getString("like"));
							item.setIsLike(jsonArr.getJSONObject(i).getString("isLike"));
							item.setNickname(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("nickname")));
							item.setComments_count(jsonArr.getJSONObject(i).getString("comments_count"));
							item.setText(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("text")));
							item.setHash(jsonArr.getJSONObject(i).getString("hash"));
							item.setDateline(jsonArr.getJSONObject(i).getString("dateline"));
							item.setTwitter_type(jsonArr.getJSONObject(i).getString("twitter_type"));
							itemlist.add(item);
						}
						Message msg = mhandler.obtainMessage(m);
						msg.obj = itemlist;
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					Logger.i(e);
					e.printStackTrace();
				}
    		}
    	}
    }
    class TimelineImportThread extends Thread{
    	private String page;
    	private String space_id;
    	private int m;
    	boolean isrun = true;
		public TimelineImportThread(String page, String space_id, int m) {
			super();
			this.page = page;
			this.space_id = space_id;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("space_id", space_id);
    			map.put("page", page);
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"space/timeline", map));
					if(jsonObj.getString("result").equals("1")){
						JSONArray jsonArr = jsonObj.getJSONArray("info");
						itemlist = new ArrayList<Timeline_item>();
						for(int i=0;i<jsonArr.length();i++){
							Timeline_item item = new Timeline_item();
							item.setAvatar(jsonArr.getJSONObject(i).getString("avatar"));
							item.setThumbnail_pic(jsonArr.getJSONObject(i).getString("thumbnail_pic"));
							item.setBmiddle_pic(jsonArr.getJSONObject(i).getString("bmiddle_pic"));
							item.setOriginal_pic(jsonArr.getJSONObject(i).getString("original_pic"));
							item.setCreate_at(jsonArr.getJSONObject(i).getString("created_at"));
							item.setIdstr(jsonArr.getJSONObject(i).getString("idstr"));
							item.setLike(jsonArr.getJSONObject(i).getString("like"));
							item.setIsLike(jsonArr.getJSONObject(i).getString("isLike"));
							item.setNickname(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("nickname")));
							item.setComments_count(jsonArr.getJSONObject(i).getString("comments_count"));
							item.setText(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("text")));
							item.setHash(jsonArr.getJSONObject(i).getString("hash"));
							item.setDateline(jsonArr.getJSONObject(i).getString("dateline"));
							item.setTwitter_type(jsonArr.getJSONObject(i).getString("twitter_type"));
							itemlist.add(item);
						}
						Message msg = mhandler.obtainMessage(m);
						msg.obj = itemlist;
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					Logger.i(e);
					e.printStackTrace();
				}
    		}
    	}
    }
    class TwitterLikeThread extends Thread{
		private String url;
    	private String hash;
    	private int m;
    	boolean isrun = true;
		public TwitterLikeThread(String url, String hash, int m) {
			super();
			this.url = url;
			this.hash = hash;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("hash", hash);
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+url, map));
					if(jsonObj.getString("result").equals("1")){
						Message msg = mhandler.obtainMessage(m);
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
    		}
    	}
	}
    
    Handler dhandler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.state = listview2.DONE;
				listview2.changeHeaderViewByState();
				if(msg.obj != null){
					ArrayList<Timeline_item> strings = (ArrayList<Timeline_item>) msg.obj;
					if(!strings.isEmpty()){
						tlitem.clear();
						tlitem.addAll(strings);
					}
					adapter.notifyDataSetChanged();
					//listview2.setSelection(0);
				}
				break;
			case 2:
				listview2.loadingAni.setVisibility(View.GONE);
				foot.setVisibility(View.INVISIBLE);
				ArrayList<Timeline_item> strings = (ArrayList<Timeline_item>) msg.obj;
				if(!strings.isEmpty()){
					adapter.ran = true;
					tlitem.addAll(strings);
					adapter.notifyDataSetChanged();
				}else{
					foot.setVisibility(View.GONE);
				}
				break;
				
			case 3:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.onRefreshComplete();
				if(msg.obj != null && ((String)msg.obj).equals("没有微博")){
					adapter.ran = false;
					//listview2.removeFooterView(footerview);
					foot.setVisibility(View.GONE);
				}else if(msg.obj != null && ((String)msg.obj).equals("没有足够权限")){
					new AlertDialog.Builder(Timeline.this).setTitle(getString(R.string.tip)).setMessage("没有足够权限查看")
					.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					}).show();
				}
				break;
			case 4:
				listview2.loadingAni.setVisibility(View.GONE);
				listview2.onRefreshComplete();
				Toast.makeText(Timeline.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
				break;
			}
		}
    };
    class DynamicThread extends Thread{
    	private String page;
    	private String space_id;
    	private int m;
    	boolean isrun = true;
		public DynamicThread(String page, String space_id, int m) {
			super();
			this.page = page;
			this.space_id = space_id;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			//itemlist.clear();
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("space_id", space_id);
    			map.put("page", page);
    			try {
    				itemlist = new ArrayList<Timeline_item>();
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"space/activity", map));
					if(jsonObj.getString("result").equals("1")){
						JSONArray jsonArr = jsonObj.getJSONArray("info");
						for(int i=0;i<jsonArr.length();i++){ 
							Timeline_item item = new  Timeline_item();
							item.setAvatar(jsonArr.getJSONObject(i).getString("avatar"));
							item.setThumbnail_pic(jsonArr.getJSONObject(i).getString("thumbnail_pic"));
							item.setBmiddle_pic(jsonArr.getJSONObject(i).getString("bmiddle_pic"));
							item.setOriginal_pic(jsonArr.getJSONObject(i).getString("original_pic"));
							item.setCreate_at(jsonArr.getJSONObject(i).getString("created_at"));
							item.setIdstr(jsonArr.getJSONObject(i).getString("idstr"));
							item.setLike(jsonArr.getJSONObject(i).getString("like"));
							item.setNickname(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("nickname")));
							item.setComments_count(jsonArr.getJSONObject(i).getString("comments_count"));
							item.setText(StringEscapeUtils.unescapeHtml4(jsonArr.getJSONObject(i).getString("text")));
							item.setHash("0");
							item.setIsLike(jsonArr.getJSONObject(i).getString("isLike"));
							item.setDateline(jsonArr.getJSONObject(i).getString("dateline"));
							item.setTwitter_type(jsonArr.getJSONObject(i).getString("twitter_type"));
							itemlist.add(item);
						}
						Message msg = dhandler.obtainMessage(m);
						msg.obj = itemlist;
						msg.sendToTarget();
					}else if(jsonObj.getString("result").equals("0")){
						Message msg = dhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						Logger.i(jsonObj.getString("msg"));
						msg.sendToTarget();
					}
					else {
						Message msg = dhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					Logger.i(e);
					e.printStackTrace();
				}
    		}
    	}
    }
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		if(position != 0 && doubleclick && !dynamicFlag){
			doubleclick = false;
			new TwitterLikeThread("twitter/like",tlitem.get(position-2).getHash(),8).start();
			if(tlitem.get(position-2).getIsLike().equals("false")){
				tlitem.get(position-2).setIsLike("true");
				tlitem.get(position-2).setLike(String.valueOf(Integer.valueOf(tlitem.get(position-2).getLike())+1));
				adapter.notifyDataSetChanged();
			}
		}
		if(position != 0 && singleclick && !fling){
			position = position - 2 ;
			Intent i = new Intent(Timeline.this,TimelineComment.class);
			i.putExtra("avater", tlitem.get(position).getAvatar());
			i.putExtra("nickname", tlitem.get(position).getNickname());
			i.putExtra("created_at", tlitem.get(position).getCreate_at());
			i.putExtra("bmiddle_pic", tlitem.get(position).getBmiddle_pic());
			i.putExtra("thumbnail_pic", tlitem.get(position).getThumbnail_pic());
			i.putExtra("text", tlitem.get(position).getText());
			i.putExtra("hash", tlitem.get(position).getHash());
			i.putExtra("idstr", tlitem.get(position).getIdstr());
			i.putExtra("comments_count", tlitem.get(position).getComments_count());
			i.putExtra("isLike", tlitem.get(position).getIsLike());
			i.putExtra("twitter_type", tlitem.get(position).getTwitter_type());
			i.putExtra("com", 0);
			startActivity(i);
		}
	}
	public boolean onDown(MotionEvent e) {
		return false;
	}
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {  
	        finish() ;
	        fling = true;
	        return true;
	    }  
		return false;
	}
	public void onLongPress(MotionEvent e) {
		
	}
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}
	public void onShowPress(MotionEvent e) {
		
	}
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
	
	public boolean onDoubleTap(MotionEvent e) {
		singleclick = false;
		doubleclick = true;
		return true;
	}
	public boolean onDoubleTapEvent(MotionEvent e) {
		return true;
	}
	public boolean onSingleTapConfirmed(MotionEvent e) {
		singleclick = true;
		return false;
	}
	Handler uhandler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				Toast.makeText(Timeline.this,"添加成功", Toast.LENGTH_SHORT).show();
				break;
			case 3:
				if(msg.obj != null){
					Toast.makeText(Timeline.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(Timeline.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 4:
				Toast.makeText(Timeline.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
				break;

			}
		}
    };
	class UpdateTwitterThread extends Thread{
		private String addTwitters;
		private String removeTwitters;
		private String space_id;
		private boolean isrun=true;
		public UpdateTwitterThread(String addTwitters, String removeTwitters,
				String space_id) {
			super();
			this.addTwitters = addTwitters;
			this.removeTwitters = removeTwitters;
			this.space_id = space_id;
		}
		@Override
		public void run() {
			while(isrun){
				isrun = false;
				Map<String, String> map = new HashMap<String, String>();
				map.put("space_id", space_id);
				map.put("addTwitters", addTwitters);
				map.put("removeTwitters", removeTwitters);
				try{
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"twitter/spacetwitterchange", map));
					if(jsonObj.getString("result").equals("1")){
						Message msg = uhandler.obtainMessage(1);
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = uhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else{
						Message msg = uhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	Handler wowohandler = new Handler(){
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				Logger.i((String)msg.obj);
				listview2.myContributionNumber.setText((String)msg.obj);
				Mywowo.isrefresh = true;
				break;
			case 3:
				if(msg.obj != null){
					Toast.makeText(Timeline.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(Timeline.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 4:
				Toast.makeText(Timeline.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
				break;

			}
		}
    };
	class GetWOWOInfoThread extends Thread{
		private String space_id;
		private boolean isrun=true;
		public GetWOWOInfoThread(
				String space_id) {
			super();
			this.space_id = space_id;
		}
		@Override
		public void run() {
			while(isrun){
				isrun = false;
				Map<String, String> map = new HashMap<String, String>();
				map.put("space_id", space_id);
				try{
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"space/info", map));
					if(jsonObj.getString("result").equals("1")){
						Message msg = wowohandler.obtainMessage(1);
						if(state.equals("1")){
							msg.obj = jsonObj.getJSONObject("info").getString("twitterCount");
							msg.sendToTarget();
						}else{
							msg.obj = jsonObj.getJSONObject("info").getJSONArray("spaceoauth").getJSONObject(0).getString("credits");
							msg.sendToTarget();
						}
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = wowohandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else{
						Message msg = wowohandler.obtainMessage(4);
						msg.sendToTarget();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
}